
Partial Class Usercontrols_Admin_UserDetails
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

	Protected m_UserID As String

#End Region

#Region " Page Events "

	Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
		Me.MessageLabel = Me.uxMessagesLabel
	End Sub

	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

		SetPageTitle("User Details")

		If IsUserAuthenticated = True Then
			m_UserID = GetStringFromQueryString("UserID")

			If Me.Page.IsPostBack = False Then
				LoadPage()
				GetRolesForUser()
			End If
		End If

		Me.uxAdminPanel.Visible = ShowControls(SecurityMask.Admin, True)

	End Sub

#End Region

#Region " Save Data "

	Private Sub SaveRolesForUser()
		Dim iI As Integer

		For iI = 0 To Me.uxRolesCheckBoxList.Items.Count - 1
			If Me.uxRolesCheckBoxList.Items(iI).Selected = True Then
				If Roles.IsUserInRole(m_UserID, Me.uxRolesCheckBoxList.Items(iI).Value.ToString) = False Then
					Roles.AddUserToRole(m_UserID, Me.uxRolesCheckBoxList.Items(iI).Value.ToString)
				End If
			Else
				If Roles.IsUserInRole(m_UserID, Me.uxRolesCheckBoxList.Items(iI).Value.ToString) = True Then
					Roles.RemoveUserFromRole(m_UserID, Me.uxRolesCheckBoxList.Items(iI).Value.ToString)
				End If
			End If
		Next
	End Sub

	Private Sub SaveValuesForUser()
		Dim User As MembershipUser

		User = Membership.GetUser(m_UserID)
		User.Email = Me.uxEmailTextBox.Text.ToString.Trim

		Membership.UpdateUser(User)
	End Sub

#End Region

#Region " Load Data "

	Private Sub LoadPage()
		Me.uxUserNameLabel.Text = Membership.GetUser(m_UserID).UserName.ToString
		Me.uxEmailTextBox.Text = Membership.GetUser(m_UserID).Email.ToString
		Me.uxJoinedOnLabel.Text = Membership.GetUser(m_UserID).CreationDate
		Me.uxLastVisitLabel.Text = Membership.GetUser(m_UserID).LastLoginDate
		Me.uxLockedOutCheckBox.Checked = Membership.GetUser(m_UserID).IsLockedOut
		Me.uxApprovedCheckBox.Checked = Membership.GetUser(m_UserID).IsApproved

		With Me.uxRolesCheckBoxList
			.DataSource = Roles.GetAllRoles
			.DataBind()
			.DataTextField = "Item"
			.DataValueField = "Item"
		End With
	End Sub

	Private Sub GetRolesForUser()
		Dim Role As New Object
		Dim iI As Integer

		For iI = 0 To Roles.GetAllRoles.Length - 1
			For Each Role In Roles.GetRolesForUser(m_UserID)
				If Me.uxRolesCheckBoxList.Items(iI).Value = Role.ToString Then
					Me.uxRolesCheckBoxList.Items(iI).Selected = True
				End If
			Next
		Next

	End Sub

#End Region

#Region " Form Controls Events "

	Protected Sub uxSaveButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxSaveButton.Click
		SaveRolesForUser()
		SaveValuesForUser()
	End Sub

	Protected Sub uxResetPasswordButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxResetPasswordButton.Click

		Dim MU As MembershipUser = Membership.Providers("AdminProvider").GetUser(m_UserID, False)

		MU.ChangePassword(MU.ResetPassword(), Me.uxNewPasswordTextbox.Text.ToString.Trim)

	End Sub

	Protected Sub uxLockedOutCheckBox_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxLockedOutCheckBox.CheckedChanged
		Dim MU As MembershipUser = Membership.Providers("AdminProvider").GetUser(m_UserID, False)

		MU.UnlockUser()

		Membership.UpdateUser(MU)

	End Sub

	Protected Sub uxApprovedCheckBox_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxApprovedCheckBox.CheckedChanged
		Dim MU As MembershipUser = Membership.Providers("AdminProvider").GetUser(m_UserID, False)

		MU.IsApproved = Me.uxApprovedCheckBox.Checked

		Membership.UpdateUser(MU)

	End Sub

#End Region

#Region " Helpers and Methods "


#End Region

End Class
